/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.service.news.impl;

import com.blandware.atleap.common.util.PartialCollection;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.news.NewsItem;
import com.blandware.atleap.persistence.core.PageDAO;
import com.blandware.atleap.persistence.exception.DeleteException;
import com.blandware.atleap.persistence.news.NewsDAO;
import com.blandware.atleap.service.core.impl.PageManagerImpl;
import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
import com.blandware.atleap.service.exception.BeanNotFoundException;
import com.blandware.atleap.service.news.NewsManager;

import java.util.Map;
import java.util.List;
import java.util.Date;
import java.util.Iterator;

/**
 * <p>Implementation of NewsManager</p>
 * <p><a href="NewsManagerImpl.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.11 $ $Date: 2008/01/13 17:20:00 $
 */
public class NewsManagerImpl extends PageManagerImpl implements NewsManager {

    /**
     * News DAO
     */
	protected NewsDAO newsDAO;
    /**
     * Page DAO
     */
	protected PageDAO pageDAO;

	/**
	 * Creates new instance of PageManagerImpl
	 */
	public NewsManagerImpl() {
	}

    /**
     * Sets DAO for operating with news items
     *
     * @param newsDAO the DAO to set
     */
	public void setNewsDAO(NewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}

    /**
     * Sets DAO for operating with pages
     *
     * @param pageDAO the DAO to set
     */
	public void setPageDAO(PageDAO pageDAO) {
		this.pageDAO = pageDAO;
	}

	//~ CRUD Methods ================================================================

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#createNewsItem(com.blandware.atleap.model.news.NewsItem, java.util.Map)
	 */
	public Long createNewsItem(NewsItem newsItem, Map linkedObjects) throws BeanAlreadyExistsException {

		if ( log.isDebugEnabled() ) {
			log.debug("Creating new news item...");
		}

		if ( pageDAO.hasDuplicates(newsItem) ) {
			// linkable item already exists
			String errorMessage = "Linkable item with URI '" + newsItem.getUri() + "' already exists";
			if ( log.isErrorEnabled() ) {
				log.error(errorMessage);
			}
			throw new BeanAlreadyExistsException(errorMessage);
		}

		// linkable item does not exist
		Long newsItemId = newsDAO.createNewsItem(newsItem, linkedObjects);
		if ( log.isDebugEnabled() ) {
			log.debug("New news item has been created successfully. Its ID is " + newsItemId);
		}
		return newsItemId;
	}

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#retrieveNewsItem(Long)
	 */
	public NewsItem retrieveNewsItem(Long newsItemId) {
		NewsItem newsItem;
		newsItem = newsDAO.retrieveNewsItem(newsItemId);

		return newsItem;
	}

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#retrieveNewsItemLite(Long)
	 */
	public NewsItem retrieveNewsItemLite(Long newsItemId) {
		return newsDAO.retrieveNewsItemLite(newsItemId);
	}

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#updateNewsItem(com.blandware.atleap.model.news.NewsItem, java.util.Map)
	 */
	public void updateNewsItem(NewsItem newsItem, Map linkedObjects) throws BeanAlreadyExistsException {

        // remove news item from cache in order to prevent Hibernate from assigning new version number
		newsDAO.removeFromCache(newsItem);

		if ( log.isDebugEnabled() ) {
			log.debug("Updating news item...");
		}

		if ( pageDAO.hasDuplicates(newsItem) ) {
			// linkable item already exists
			String errorMessage = "Linkable item with URI '" + newsItem.getUri() + "' already exists";
			if ( log.isErrorEnabled() ) {
				log.error(errorMessage);
			}
			throw new BeanAlreadyExistsException(errorMessage);
		}

		// linkable item does not exist
		newsDAO.updateNewsItem(newsItem, linkedObjects);

		if ( log.isDebugEnabled() ) {
			log.debug("News item was updated successfully.");
		}
	}

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#updateNewsItem(com.blandware.atleap.model.news.NewsItem) 
	 */
	public void updateNewsItem(NewsItem newsItem) throws BeanAlreadyExistsException {
		updateNewsItem(newsItem, null);
	}

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#deleteNewsItem(Long)
	 */
	public void deleteNewsItem(Long newsItemId) throws DeleteException, BeanNotFoundException {
		NewsItem newsItem = newsDAO.retrieveNewsItem(newsItemId);
		if ( newsItem == null ) {
			String errorMessage = "News item with ID=" + newsItemId + " does not exist";
			throw new BeanNotFoundException(errorMessage);
		}
		newsDAO.deleteNewsItem(newsItem);
		if ( log.isDebugEnabled() ) {
			log.debug("Deleted news item: ID=" + newsItemId);
		}
	}

	// ~ Additional methods ================================================================

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#listNewsItems(com.blandware.atleap.common.util.QueryInfo)
	 */
	public PartialCollection listNewsItems(QueryInfo queryInfo) {
		return newsDAO.listNewsItems(queryInfo);
	}

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#getLastNews(com.blandware.atleap.common.util.QueryInfo) 
	 */
	public List getLastNews(QueryInfo queryInfo) {
		return newsDAO.getLastNews(queryInfo);
	}

	/**
	 * @see com.blandware.atleap.service.news.NewsManager#performActivityCheck()
	 */
	public void performActivityCheck() {
		if ( log.isInfoEnabled() ) {
			log.info("Performing news activation check...");
		}
		Date today = new Date();
        List unpublishedNews = newsDAO.findUnpublishedNews(today);
		if ( log.isInfoEnabled() ) {
			log.info("Number of news to set active: " + unpublishedNews.size());
		}
		for ( Iterator i = unpublishedNews.iterator(); i.hasNext(); ) {
			NewsItem item = (NewsItem) i.next();
			item.setActive(Boolean.TRUE);
		}

		List expiredNews = newsDAO.findExpiredNews(today);
		if ( log.isInfoEnabled() ) {
			log.info("Number of news to set inactive: " + expiredNews.size());
		}
		for ( Iterator i = expiredNews.iterator(); i.hasNext(); ) {
			NewsItem item = (NewsItem) i.next();
			item.setActive(Boolean.FALSE);
		}

	}

}
